package com.example.cinema.request;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.util.Log;

import com.example.cinema.UserAttribute;
import com.example.cinema.bindingdata.Advert;
import com.example.cinema.bindingdata.StartData;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class RequestInitData {

    //地址
    private String url;

    public RequestInitData(Context context) {
        try {
            ApplicationInfo info = context.getPackageManager()
                    .getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
            url = info.metaData.getString("url");
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

    //黑名单检测
    public boolean BlackCheck(String dev) {
        boolean b = false;
        Log.i("KKKK", dev);
        OkHttpClient client = new OkHttpClient();
        FormBody formBody = new FormBody.Builder().add("dev", dev).build();
        Request request = new Request.Builder().url(url + "/com/isdevice").post(formBody).build();
        try {
            Response response = client.newCall(request).execute();
            String codes = response.body().string();
            Log.i("KKKK", codes);
            if (codes.equals("200")) {
                b = true;
            } else {
                b = false;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return b;
    }

    //启动广告数据
    public JSONArray GetAdvert() {
        Log.i("KKKK", "执行请求1");
        Log.i("KKKK", url + "/advert");
        JSONArray object = null;
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().url(url + "/com/advert").get().build();
        try {
            Log.i("KKKK", "执行请求2");
            Response response = client.newCall(request).execute();
            String body = response.body().string();
            Log.i("KKKK", body);
            JSONObject JO = new JSONObject(body);
            object = JO.getJSONArray("data");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return object;
    }

    //获取海报
    public JSONArray GetPoster() {
        JSONArray jsonArray = null;
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().get().url(url + "/com/poster").build();
        try {
            Response response = client.newCall(request).execute();
            String bodys = response.body().string();
            JSONObject JO = new JSONObject(bodys);
            jsonArray = JO.getJSONArray("data");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jsonArray;
    }

    //获取分类
    public JSONArray GetType() {
        JSONArray array = null;
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().url(url + "/com/gettypes").get().build();
        try {
            Response response = client.newCall(request).execute();
            String body = response.body().string();
            if (body == null) {
                return null;
            }
            JSONObject object = new JSONObject(body);
            array = object.getJSONArray("data");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return array;
    }

    //获取短视频数据
    public JSONArray GetShortData() {
        JSONArray array = null;
        OkHttpClient client = new OkHttpClient();
        FormBody formBody = new FormBody.Builder()
                .add("page", "1").add("limit", "15")
                .add("type", "1024").build();
        Request request = new Request.Builder().post(formBody).url(url + "/com/typevideo").build();
        try {
            Response response = client.newCall(request).execute();
            String body = response.body().string();
            if (body == null) {
                return null;
            }
            Log.i("SSSS", body);
            JSONObject object = new JSONObject(body);
            array = object.getJSONArray("data");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return array;
    }
    //获取公告
    public JSONObject GetNotice()
    {
        JSONObject Jo = null;
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().get().url(url+"/com/notice").build();
        try {
            Response response = client.newCall(request).execute();

            String bod = response.body().string();
            Log.i("SSSSS",bod);
            JSONObject JOO  = new JSONObject(bod);
            Jo = JOO.getJSONObject("data");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return Jo;
    }
    //获取直播数据
    public JSONArray GetLiveData()
    {
        JSONArray Jo = null;
        OkHttpClient client = new OkHttpClient();
        FormBody formBody = new FormBody.Builder().add("page","1").add("limit","10").build();
        Request request = new Request.Builder().url(url+"/com/relive").post(formBody).build();
        try {
            Response response = client.newCall(request).execute();
            String bod = response.body().string();

            JSONObject jsonObject = new JSONObject(bod);
            Jo = jsonObject.getJSONArray("data");
        } catch (IOException | JSONException e) {
            e.printStackTrace();
        }
        return Jo;
    }
    //获取下载地址以及背景图
    public JSONObject GetDowUrl()
    {
        JSONObject jo = null;
        OkHttpClient client = new OkHttpClient();
        Request request =new Request.Builder().url(url +"/com/getdow").get().build();
        try {
            Response response = client.newCall(request).execute();
            String bod = response.body().string();
            jo = new JSONObject(bod);
        } catch (IOException | JSONException e) {
            e.printStackTrace();
        }
        return jo;
    }
    //获取VIP充值金额数据
    public JSONArray GetMoney()
    {
        JSONArray array = null;
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().url(url+"/com/viprecharge").get().build();
        try {
            Response response = client.newCall(request).execute();
            String bod = response.body().string();
            Log.i("TTTTTTT",bod);
            array = new JSONArray(bod);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return array;
    }

    /**
     * 获取模块页广告
     * @return
     */
    public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
    public List<Advert> GetAdverXq()
    {
        List<Advert> list = new ArrayList<>();
        try {
            OkHttpClient client = new OkHttpClient();

            Request request = new Request.Builder().url(url+"/com/getdetails").get().build();
            Response response = client.newCall(request).execute();
            String bod = response.body().string();
            JSONObject object = new JSONObject(bod);
            JSONArray array = object.getJSONArray("data");
            for (int i=0;i<array.length();i++)
            {
                Advert advert = new Advert();
                advert.adverskip = array.getJSONObject(i).getString("adverskip");
                advert.adverurl = array.getJSONObject(i).getString("adverurl");
                list.add(advert);
            }
        } catch (IOException | JSONException e) {
            e.printStackTrace();
        }
        return list;
    }
    /**
     * 获取播放页广告
     * @return
     */
    public List<Advert> GetAdverPlay()
    {
        List<Advert> list = new ArrayList<>();
        try {
            OkHttpClient client = new OkHttpClient();
            Request request = new Request.Builder().url(url+"/com/getplayadv").get().build();
            Response response = client.newCall(request).execute();
            String bod = response.body().string();
            JSONObject object = new JSONObject(bod);
            JSONArray array = object.getJSONArray("data");
            for (int i=0;i<array.length();i++)
            {
                Advert advert = new Advert();
                advert.adverskip = array.getJSONObject(i).getString("adverskip");
                advert.adverurl = array.getJSONObject(i).getString("adverurl");
                list.add(advert);
            }
        } catch (IOException | JSONException e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 获取APP配置
     * @param attribute
     */
    public void GetAppConfig(UserAttribute attribute) {
        try {
            OkHttpClient client = new OkHttpClient();
            Request request = new Request.Builder().url(url + "/com/appconfig").get().build();
            Response response = client.newCall(request).execute();
            String bod = response.body().string();
            Log.i("KDDD",bod);
            JSONObject jsonObject = new JSONObject(bod);
            JSONObject mmode = jsonObject.getJSONObject("mmode");
            int moodsw = mmode.getInt("switchs");
            attribute.setAppMoodMode(moodsw);
            JSONObject vmode = jsonObject.getJSONObject("vmode");
            int videsw = vmode.getInt("switchs");
            attribute.setAppVideoUpMode(videsw);
            JSONObject pmode = jsonObject.getJSONObject("pmode");
            int paysw =  pmode.getInt("models");
            attribute.setAppMoneyMode(paysw);
            JSONObject amode = jsonObject.getJSONObject("amode");
            int appsw =  amode.getInt("model");
            attribute.setAppMode(appsw);
            String url = jsonObject.getString("shopurl");
            Log.i("SDWWW",url);
            attribute.setPlayUrl(url);
        } catch (IOException | JSONException e) {
            e.printStackTrace();
        }
    }
}
